/**
 * 
 */
package dt.inlee.system.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.interceptor.validation.SkipValidation;
import org.apache.struts2.json.annotations.JSON;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import dt.inlee.common.constant.CommonConstant;
import dt.inlee.common.exception.FrameworkException;
import dt.inlee.common.util.BaseUtil;
import dt.inlee.common.vo.PageBean;
import dt.inlee.domain.DesktopDefend;
import dt.inlee.domain.Message;
import dt.inlee.domain.SelfDesktopDefend;
import dt.inlee.system.service.DesktopDefendService;
import dt.inlee.system.service.MessageService;
import dt.inlee.system.vo.DesktopDefendBean;
import dt.inlee.system.vo.FriendlyLinkBean;
import dt.inlee.system.vo.MessageBean;
import dt.inlee.system.vo.SysUserLoginBean;

/**
 * @功能说明：桌面管理控制层
 * @作者： xgyin
 * @创建日期： 2010-12-22
 */
@Scope("prototype")
@Results( { @Result(name = "success", type = "json") })
@Controller("desktopDefendAction")
public class DesktopDefendAction extends ActionSupport {
	private static final long serialVersionUID = -7085101001772580445L;
	@SuppressWarnings("unused")
    private static Logger logger = Logger.getLogger(DesktopDefendAction.class);
	@Autowired
	private DesktopDefendService desktopDefendService;
	@Autowired
	private MessageService messageService;

	//显示自己的桌面
	private Map<String, List<DesktopDefendBean>> centerMap = new TreeMap<String, List<DesktopDefendBean>>();
	//桌面维护
	private List<DesktopDefendBean> desktopDefendBeans = new ArrayList<DesktopDefendBean>();
	//代办
	private List<MessageBean> messageBeans = new ArrayList<MessageBean>();

	//友情链接
	private List<FriendlyLinkBean> friendlyLinkBeans = new ArrayList<FriendlyLinkBean>();
	private DesktopDefendBean desktopDefendBean = new DesktopDefendBean();
	private PageBean pageBean = new PageBean();
	//处理结果
	private boolean result = false;
	//自己的桌面菜单区域块的位置信息
	private String placeId;
	//自己菜单维护ID
	private String selfDesktopDenfendId;

	public List<DesktopDefendBean> getDesktopDefendBeans() {
		return desktopDefendBeans;
	}

	public void setDesktopDefendBeans(List<DesktopDefendBean> desktopDefendBeans) {
		this.desktopDefendBeans = desktopDefendBeans;
	}

	@JSON(serialize = false)
	public DesktopDefendBean getDesktopDefendBean() {
		return desktopDefendBean;
	}

	public void setDesktopDefendBean(DesktopDefendBean desktopDefendBean) {
		this.desktopDefendBean = desktopDefendBean;
	}

	public PageBean getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}

	public boolean isResult() {
		return result;
	}

	public void setResult(boolean result) {
		this.result = result;
	}

	@JSON(serialize = false)
	public Map<String, List<DesktopDefendBean>> getCenterMap() {
		return centerMap;
	}

	public void setCenterMap(Map<String, List<DesktopDefendBean>> centerMap) {
		this.centerMap = centerMap;
	}

	public List<MessageBean> getMessageBeans() {
		return messageBeans;
	}

	public void setMessageBeans(List<MessageBean> messageBeans) {
		this.messageBeans = messageBeans;
	}

	@JSON(serialize = false)
	public String getPlaceId() {
		return placeId;
	}

	public void setPlaceId(String placeId) {
		this.placeId = placeId;
	}

	@JSON(serialize = false)
	public String getSelfDesktopDenfendId() {
		return selfDesktopDenfendId;
	}

	public void setSelfDesktopDenfendId(String selfDesktopDenfendId) {
		this.selfDesktopDenfendId = selfDesktopDenfendId;
	}

	public List<FriendlyLinkBean> getFriendlyLinkBeans() {
		return friendlyLinkBeans;
	}

	public void setFriendlyLinkBeans(List<FriendlyLinkBean> friendlyLinkBeans) {
		this.friendlyLinkBeans = friendlyLinkBeans;
	}

	@Action(value = "findAlldesktopDefends", results = { @Result(type = "json") })
	@SkipValidation()
	public String findAlldesktopDefends() throws FrameworkException, Exception {
		//jqGrid查询转码-utf-8
		BaseUtil.decodeObject(this.desktopDefendBean);
		this.pageBean.init(this.desktopDefendService.getDesktopDefendTotalRows(this.desktopDefendBean.getTitle(),
				this.desktopDefendBean.getDivId()));
		List<DesktopDefend> desktopDefends = this.desktopDefendService
				.findAllDesktopDefends(this.desktopDefendBean.getTitle(), this.desktopDefendBean.getDivId(),
						this.pageBean.getCurPage(), this.pageBean.getPageSize());
		for (DesktopDefend defend : desktopDefends) {
			DesktopDefendBean bean = new DesktopDefendBean();
			BeanUtils.copyProperties(defend, bean);
			this.desktopDefendBeans.add(bean);
		}
		return SUCCESS;
	}

	/**
	 * 通过桌面管理ID删除桌面管理信息
	 * @return
	 * @throws FrameworkException
	 */
	@Action(value = "deleteDesktopDefends", results = { @Result(name = "success", type = "chain", location = "findAlldesktopDefends") })
	@SkipValidation()
	public String deleteDesktopDefends() throws FrameworkException {
		this.desktopDefendService.deleteDesktopDefends(this.desktopDefendBean.getDeleteIDs().split(","));
		return SUCCESS;
	}

	/**
	 * 通过桌面管理ID获取桌面管理信息
	 * @return
	 * @throws FrameworkException
	 */
	@Action(value = "findDesktopDefend", results = {
			@Result(name = "success", location = "/page/system/desktop_manage_edit.jsp"),
			@Result(name = "input", location = "/page/system/desktop_manage_list.jsp") })
	@SkipValidation()
	public String findDesktopDefend() throws FrameworkException, Exception {
		DesktopDefend desktopDefend = this.desktopDefendService.findDesktopDefendById(this.desktopDefendBean.getId());
		BeanUtils.copyProperties(desktopDefend, this.desktopDefendBean);
		return SUCCESS;
	}

	/**
	 * 进入桌面管理添加页面
	 * @return
	 * @throws FrameworkException
	 */
	@Action(value = "createDesktopDefend", results = @Result(name = "success", location = "/page/system/desktop_manage_edit.jsp"))
	@SkipValidation()
	public String createDesktopDefend() throws FrameworkException {
		return SUCCESS;
	}

	/**
	 * 添加桌面管理
	 * @return
	 * @throws FrameworkException
	 */
	@Action(value = "saveDesktopDefend", results = {
			@Result(name = "success", location = "/page/system/desktop_manage_list.jsp"),
			@Result(name = "input", location = "/page/system/desktop_manage_edit.jsp") })
	public String saveDesktopDefend() throws FrameworkException, Exception {
		if (!BaseUtil.isEmpty(this.desktopDefendService.findDesktopDefendyByName(this.desktopDefendBean.getTitle(),
				this.desktopDefendBean.getDivId()))) {
			this.addFieldError("nameAndCnName", "已有区域业务名称或DIV标签ID!");
			return INPUT;
		}
		DesktopDefend desktopDefend = new DesktopDefend();
		BeanUtils.copyProperties(this.desktopDefendBean, desktopDefend);
		this.desktopDefendService.saveDesktopDefend(desktopDefend);
		return SUCCESS;
	}

	/**
	 * 修改桌面管理
	 * @return
	 * @throws FrameworkException
	 */
	@Action(value = "modifyDesktopDefend", results = {
			@Result(name = "success", location = "/page/system/desktop_manage_list.jsp"),
			@Result(name = "input", location = "/page/system/desktop_manage_edit.jsp") })
	public String modifyDesktopDefend() throws FrameworkException, Exception {
		if (!BaseUtil.isEmpty(this.desktopDefendService.findDesktopDefendByCon(this.desktopDefendBean.getId(),
				this.desktopDefendBean.getTitle(), this.desktopDefendBean.getDivId()))) {
			this.addFieldError("nameAndCnName", "已有区域业务名称或DIV标签ID!");
			return INPUT;
		}
		DesktopDefend desktopDefend = this.desktopDefendService.findDesktopDefendById(this.desktopDefendBean.getId());
		BeanUtils.copyProperties(this.desktopDefendBean, desktopDefend);
		this.desktopDefendService.updateDesktopDefend(desktopDefend);
		return SUCCESS;
	}

	@Action(value = "findSelfDesktopDefends", results = { @Result(type = "json") })
	@SkipValidation()
	public String findSelfDesktopDefends() throws FrameworkException {
		List<DesktopDefend> desktopDefends = this.desktopDefendService.findSelfDesktopDefends();
		for (DesktopDefend defend : desktopDefends) {
			DesktopDefendBean bean = new DesktopDefendBean();
			BeanUtils.copyProperties(defend, bean);
			this.desktopDefendBeans.add(bean);
		}
		return SUCCESS;

	}

	/**
	 * 设置自己的桌面菜单
	 * @return
	 * @throws FrameworkException
	 */
	@Action(value = "setSelfDesktopDefends", results = { @Result(type = "json", params = { "excludeNullProperties",
			"true", "includeProperties", "result" }) })
	public String setSelfDesktopDefends() throws FrameworkException {
		this.desktopDefendService.setSelfDesktopDefends(BaseUtil.isEmpty(this.desktopDefendBean.getDeleteIDs()) ? null
				: this.desktopDefendBean.getDeleteIDs().split(","));
		result = true;
		return SUCCESS;

	}

	/**
	 * 改变桌面区域块的位置
	 * @return
	 * @throws FrameworkException
	 */
	@Action(value = "setDesktopPositionds", results = { @Result(type = "json") })
	@SkipValidation()
	public String setDesktopPositionds() throws FrameworkException {
		this.desktopDefendService.setDesktopPositionds(this.placeId);
		return SUCCESS;
	}

	/**
	 * 删除自己桌面的桌面项
	 * @return
	 * @throws FrameworkException
	 */
	@Action(value = "deleteSelfDesktopDefendById", results = { @Result(type = "json") })
	@SkipValidation()
	public String deleteSelfDesktopDefendById() throws FrameworkException {
		this.desktopDefendService.deleteSelfDesktopDefendById(this.selfDesktopDenfendId);
		return SUCCESS;

	}

	/**
	 * 显示主页
	 * @return
	 * @throws FrameworkException
	 */
	@Action(value = "desktopCenter", results = { @Result(name = "success", location = "/page/center.jsp"),
			@Result(name = "input", location = "/page/center.jsp") })
	public String desktopCenter() throws FrameworkException {
		List<SelfDesktopDefend> desktopDefends = this.desktopDefendService.findSelfDesktopCenters();
		this.centerMap.put("1", new ArrayList<DesktopDefendBean>());
		this.centerMap.put("2", new ArrayList<DesktopDefendBean>());
		for (SelfDesktopDefend selfDefind : desktopDefends) {
			DesktopDefend defend = selfDefind.getDesktopDefend();
			DesktopDefendBean bean = new DesktopDefendBean();
			bean.setId(selfDefind.getId());
			bean.setDefaultPosition(selfDefind.getDefaultPosition());
			bean.setDivId(defend.getDivId());
			bean.setRemark(defend.getRemark());
			bean.setTitle(defend.getTitle());
			List<DesktopDefendBean> beans = this.centerMap.get(bean.getDefaultPosition());
			beans.add(bean);
			this.centerMap.put(bean.getDefaultPosition(), beans);

		}
		return SUCCESS;

	}

	/**
	 * 桌面代办事项
	 * @return
	 * @throws FrameworkException
	 */
	@Action(value = "findDesktopMessages", results = { @Result(type = "json") })
	@SkipValidation()
	public String findDesktopMessages() throws FrameworkException {
		SysUserLoginBean loginUser = (SysUserLoginBean) ActionContext.getContext().getSession().get(
				CommonConstant.SESSION_USER);
		List<Message> msgs = this.messageService.getPersonNotReadMsg(loginUser.getId()+"");
		for (Message msg : msgs) {
			MessageBean bean = new MessageBean();
			BeanUtils.copyProperties(msg, bean);
			this.messageBeans.add(bean);
		}
		return SUCCESS;

	}

	/**
	 * 桌面公告
	 * @return
	 * @throws FrameworkException
	 */
	@Action(value = "findDesktopNews", results = { @Result(type = "json") })
	@SkipValidation()
	public String findDesktopNews() throws FrameworkException {

		return SUCCESS;

	}
}
